package com.llb666.e.shop.api.controller.center;

import com.llb666.e.shop.api.controller.BaseController;
import com.llb666.e.shop.api.vo.OrderItemsVo;
import com.llb666.e.shop.api.vo.OrderStatusCountsVo;
import com.llb666.e.shop.domain.entity.OrderStatus;
import com.llb666.e.shop.domain.service.MyOrdersService;
import com.llb666.e.shop.infracore.constants.AppConstant;
import com.llb666.e.shop.infracore.web.BaseResponse;
import com.llb666.e.shop.infracore.web.DataResponse;
import com.llb666.e.shop.infracore.web.PagedGridResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author llb666
 */
@Tag(name = "用户中心我的订单", description = "用户中心我的订单相关接口")
@RestController
@RequestMapping("myorders")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyOrdersController extends BaseController {

    private final MyOrdersService myOrdersService;

    @Operation(summary = "获得订单状态数概况", description = "获得订单状态数概况", method = "POST")
    @PostMapping("/statusCounts")
    public BaseResponse statusCounts(
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return DataResponse.errorMsg(null);
        }
        OrderStatusCountsVo result = myOrdersService.getOrderStatusCounts(userId);
        return DataResponse.ok(result);
    }

    @Operation(summary = "查询订单列表", description = "查询订单列表", method = "POST")
    @PostMapping("/query")
    public BaseResponse query(
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId,
            @Parameter(name = "orderStatus", description = "订单状态") Integer orderStatus,
            @Parameter(name = "page", description = "查询下一页的第几页") Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数") Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return DataResponse.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = AppConstant.COMMON_PAGE_SIZE;
        }
        PagedGridResult<?> grid = myOrdersService.queryMyOrders(userId, orderStatus, page, pageSize);
        return DataResponse.ok(grid);
    }

    @Operation(summary = "商家发货", description = "商家发货没有后端，所以这个接口仅仅只是用于模拟", method = "GET")
    @GetMapping("/deliver")
    public BaseResponse deliver(
            @Parameter(name = "orderId", description = "订单id", required = true)
            @RequestParam String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return DataResponse.errorMsg("订单ID不能为空");
        }
        myOrdersService.updateDeliverOrderStatus(orderId);
        return BaseResponse.ok();
    }

    @Operation(summary = "用户确认收货", description = "用户确认收货", method = "POST")
    @PostMapping("/confirmReceive")
    public BaseResponse confirmReceive(
            @Parameter(name = "orderId", description = "订单id", required = true)
            @RequestParam String orderId,
            @Parameter(name = "userId", description = "用户id", required = true)
            @RequestParam String userId) {
        // 验证用户和订单是否有关联关系，避免非法用户调用.
        checkUserOrder(userId, orderId);
        boolean res = myOrdersService.updateReceiveOrderStatus(orderId);
        if (!res) {
            return DataResponse.errorMsg("订单确认收货失败！");
        }
        return BaseResponse.ok();
    }

    @Operation(summary = "用户删除订单", description = "用户删除订单", method = "POST")
    @PostMapping("/delete")
    public BaseResponse delete(
            @Parameter(name = "orderId", description = "订单id", required = true)
            @RequestParam String orderId,
            @Parameter(name = "userId", description = "用户id", required = true)
            @RequestParam String userId) {
        // 验证用户和订单是否有关联关系，避免非法用户调用.
        checkUserOrder(userId, orderId);
        boolean res = myOrdersService.deleteOrder(userId, orderId);
        if (!res) {
            return DataResponse.errorMsg("订单删除失败！");
        }
        return BaseResponse.ok();
    }

    @Operation(summary = "查询订单动向", description = "查询订单动向", method = "POST")
    @PostMapping("/trend")
    public BaseResponse trend(
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId,
            @Parameter(name = "page", description = "查询下一页的第几页") Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数") Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return DataResponse.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = AppConstant.COMMON_PAGE_SIZE;
        }
        PagedGridResult<OrderStatus> grid = myOrdersService.getOrdersTrend(userId, page, pageSize);
        return DataResponse.ok(grid);
    }


    @Operation(summary = "查询订单列表", description = "查询订单列表", method = "POST")
    @PostMapping("/pending")
    public BaseResponse pending(
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId,
            @Parameter(name = "orderId", description = "订单id", required = true) @RequestParam String orderId) {
        // 判断用户和订单是否关联
        checkUserOrder(userId, orderId);
        List<OrderItemsVo> list = OrderItemsVo.Converter.INSTANCE.do2VoList(
                myOrdersService.queryPendingComment(orderId));
        return DataResponse.ok(list);
    }

}
